﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace Pibby.Core
{
	public class PodcastCollection : Collection<Podcast>
	{
		#region Indexing

		public Podcast this[string id]
		{
			get
			{
				return this.Where(podcast => podcast.Id == id)
							.FirstOrDefault();
			}
		}

		#endregion

		#region Addition

		public void AddRange(IEnumerable<Podcast> collection)
		{
			foreach (Podcast podcast in collection)
			{
				base.Add(podcast);
			}
		}

		#endregion

		public void Sort()
		{
			List<Podcast> items = (List<Podcast>)this.Items;

			// sort descending
			items.Sort(delegate(Podcast p1, Podcast p2) { return p2.Score - p1.Score; });
		}

		public void SortAlphabetically()
		{
			List<Podcast> items = (List<Podcast>)this.Items;

			// sort descending
			items.Sort(delegate(Podcast p1, Podcast p2) 
			{
				return p1.Name.CompareTo(p2.Name);
			});
		}

		public void SortByWhuffie()
		{
			List<Podcast> items = (List<Podcast>)this.Items;

			// sort descending
			items.Sort(delegate(Podcast p1, Podcast p2)
			{
				return p1.Whuffie.Score.CompareTo(p2.Whuffie.Score);
			});
		}

		public Podcast FindByName(string name)
		{
			return this.Where(podcast => podcast.SafeName == name)
							.FirstOrDefault();
		}
	}
}
